﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace XML
{
    public class RolesDal
    {
        #region  BasicMethod

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("role_id", "roles");
        }

        public bool Exists(int role_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from roles");
            strSql.Append(" where role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = role_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Add(Model.roles model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into roles(");
            strSql.Append("role_id,role_name,role_type,country_format_vid)");
            strSql.Append(" values (");
            strSql.Append("@role_id,@role_name,@role_type,@country_format_vid)");
            SqlParameter[] parameters = {
					new SqlParameter("@role_id", SqlDbType.Int,4),
					new SqlParameter("@role_name", SqlDbType.VarChar,50),
					new SqlParameter("@role_type", SqlDbType.VarChar,10),
					new SqlParameter("@country_format_vid", SqlDbType.Int,4)};
            parameters[0].Value = model.role_id;
            parameters[1].Value = model.role_name;
            parameters[2].Value = model.role_type;
            parameters[3].Value = model.country_format_vid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(Model.roles model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update roles set ");
            strSql.Append("role_name=@role_name,");
            strSql.Append("role_type=@role_type,");
            strSql.Append("country_format_vid=@country_format_vid");
            strSql.Append(" where role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@role_name", SqlDbType.VarChar,50),
					new SqlParameter("@role_type", SqlDbType.VarChar,10),
					new SqlParameter("@country_format_vid", SqlDbType.Int,4),
					new SqlParameter("@role_id", SqlDbType.Int,4)};
            parameters[0].Value = model.role_name;
            parameters[1].Value = model.role_type;
            parameters[2].Value = model.country_format_vid;
            parameters[3].Value = model.role_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Delete(int role_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from roles ");
            strSql.Append(" where role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = role_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteList(string role_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from roles ");
            strSql.Append(" where role_id in (" + role_idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public Model.roles GetModel(int role_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 role_id,role_name,role_type,country_format_vid from roles ");
            strSql.Append(" where role_id=@role_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@role_id", SqlDbType.Int,4)			};
            parameters[0].Value = role_id;

            Model.roles model = new Model.roles();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public Model.roles DataRowToModel(DataRow row)
        {
            Model.roles model = new Model.roles();
            if (row != null)
            {
                if (row["role_id"] != null && row["role_id"].ToString() != "")
                {
                    model.role_id = int.Parse(row["role_id"].ToString());
                }
                if (row["role_name"] != null)
                {
                    model.role_name = row["role_name"].ToString();
                }
                if (row["role_type"] != null)
                {
                    model.role_type = row["role_type"].ToString() == RoleType.Personal.ToString() ? RoleType.Personal : RoleType.Bussiness;
                }
                if (row["country_format_vid"] != null && row["country_format_vid"].ToString() != "")
                {
                    model.country_format_vid = int.Parse(row["country_format_vid"].ToString());
                }
            }
            return model;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select role_id,role_name,role_type,country_format_vid ");
            strSql.Append(" FROM roles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" role_id,role_name,role_type,country_format_vid ");
            strSql.Append(" FROM roles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM roles ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.role_id desc");
            }
            strSql.Append(")AS Row, T.*  from roles T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        public List<Model.roles> DataTableToList(DataTable dt)
        {
            List<Model.roles> modelList = new List<Model.roles>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.roles model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  BasicMethod

    }
}

